<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo - Libraries - MySQL Queries File
*
* @package		Ineo
* @subpackage   Libraries
* @author		Dan Cryer & William McGann
* @copyright	Dan Cryer & William McGann
* @version		1.0.0
*/

class ineo_lib_mysql
{
	/**
	* Variable Setup:
	*/
	var $core;
	var $ipsclass;
	
	/**
	* Constructor:
	*/
	function ineo_lib_mysql( &$ineo )
	{
		$this->core     =& $ineo;
		$this->ipsclass =& $ineo->ipsclass;
	}
	
	/**
	* Get the top rated articles from the db, with a defined limit
	*/
	function query_get_top_rated( $limit )
	{
		return "SELECT a.article_id,a.article_title,a.article_text_id, SUM(r.rate_rating) AS rate_total, COUNT(r.rate_artid) AS rate_count, AVG( r.rate_rating ) AS rate_top_rating FROM ibf_ineo_articles a  LEFT JOIN ibf_ineo_rating r ON ( r.rate_artid = a.article_id )  WHERE article_is_queued = '0' GROUP BY r.rate_artid ORDER BY rate_top_rating DESC, rate_count DESC LIMIT " . $limit;
	}
	
	function check_bool_mode()
	{
		// Can we use Boolean Mode?
		$this->ipsclass->DB->sql_get_version();
		$this->boolean_mode = ( $this->ipsclass->DB->mysql_version >= 40010 ) ? 'IN BOOLEAN MODE' : '';
	}
	
	/**
	* Get the results for a "full" search using MySQL's fulltext searching
	*/
	function query_full_search_count($keywords,$queued,$categories)
	{		
		return "SELECT count( c.content_content ) as result_count FROM ".SQL_PREFIX."ineo_content c LEFT JOIN ".SQL_PREFIX."ineo_articles a ON ( a.article_content = c.content_id ) WHERE MATCH( c.content_content ) AGAINST ('{$keywords}' {$this->boolean_mode}) AND a.article_title IS NOT NULL {$queued}{$categories}";
	}
	
	/**
	* Perform a "full" search using MySQL's fulltext searching
	*/
	function query_full_search($keywords,$queued,$categories,$start,$order)
	{
		return "SELECT MATCH( c.content_content ) AGAINST ('{$keywords}' {$this->boolean_mode}) as relevance, c.*, a.* FROM ".SQL_PREFIX."ineo_content c LEFT JOIN ".SQL_PREFIX."ineo_articles a ON ( a.article_content = c.content_id ) WHERE MATCH( c.content_content ) AGAINST ('{$keywords}' {$this->boolean_mode}) AND a.article_title IS NOT NULL {$queued}{$categories} ORDER BY {$order} DESC LIMIT {$start},20";
	}
	
	function query_full_search_categories( $categories )
	{
		return in_array('0',$categories) ? '' : ' AND a.article_cat_id IN (0,'.$this->safe_int_ids($categories).')';
	}
}

?>